<template>
  <a-modal
    title="換表"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <template slot="footer">
        <a-button key="back" @click="handleCancel">
          取消
        </a-button>
        <a-button key="submit" type="primary"  @click="handleSubmit">
          保存抄錶
        </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false">
         <a-input  v-decorator="['id']" />
        </a-form-item>
        <a-form-item
          label="新錶讀數"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        <a-input-number
                style="width:100%"
                :precision="2"
                :min="0"
                :max="99999999"
                v-decorator="['newCost', { initialValue: 0, rules: [{required: true, message: '请输入新錶讀數！'}]}]"
                :step="10"/>
        </a-form-item>
        <a-form-item
          label="新錶讀數"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :disabled="true">
         <a-input  v-decorator="['zdqs']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { getMeterListByStateIdOnJzcbForOne, hydropowerMeterhuanAddOne } from '@/api/modular/main/hydropowermeter/hydropowerMeterManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        meterReadingDateString: '',
        form: this.$form.createForm(this),
        visible: false,
        confirmLoading: false,
        memberLoading: false,
        dataMeter: [],
        data: {}
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (leaseId, id) {
        this.visible = true
        this.getSdFyListByDetail(leaseId, id)
      },
      getSdFyListByDetail(leaseId, id) {
        getMeterListByStateIdOnJzcbForOne(leaseId, id).then((res) => {
              if (res.code === 200) {
                this.data = res.data
                 setTimeout(() => {
                  this.form.setFieldsValue(
                    {
                      id: this.data.id,
                      newCost: this.data.newCost,
                      zdqs: this.data.zdqs
                    }
                  )
                }, 100)
                this.confirmLoading = false
              } else {
                this.$message.error('查詢失敗')
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
           validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            hydropowerMeterhuanAddOne(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('換表成功！')
                this.confirmLoading = false
                this.$emit('ok', '')
                this.handleCancel()
              } else {
                this.$message.error(res.msg)//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
        })
          } else {
             this.confirmLoading = false
          }
        })
      },
      onChangerentedDay(date, dateString) {
        this.meterReadingDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.data = {}
        this.dataMeter = []
        this.visible = false
      }
    }
  }
</script>
